package com.hibernate.db;

import java.util.Set;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.hibernate.domain.Classes;
import com.hibernate.domain.Student;
import com.hibernate.utils.HibernateUtils;

public class One2ManyDoubleTest extends HibernateUtils {
	
	/**
	 * 保存学生级联保存班级
	 */
	@Test
	public void testSaveStudents_Cascade_SaveClasses() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Classes classes = new Classes() ;
		classes.setCname("1班") ;
		classes.setDescription("培训班") ;
		Student student01 = new Student("张三","培训班学生") ;
		//学生关联班级
		student01.setClasses(classes) ;
		//保存学生，级联保存班级
		session.save(student01) ;
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 更新学生，级联更新班级
	 */
	@Test
	public void testUpdateStudent_Cascade_UpdateClasses() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		
		Student student = (Student)session.get(Student.class, 4L) ;
		student.setDescription("培训班学生") ;
		
		Classes classes = student.getClasses() ;
		classes.setDescription("培训班") ;
		
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 新建一个学生，并且和一个班级关联
	 */
	@Test
	public void testSaveStudent_BuildR() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Student student02 = new Student("琪琪","培训班学生") ;
		Classes classes = (Classes)session.get(Classes.class, 1L) ;
		//将student02关联到classes中
		student02.setClasses(classes) ;
		session.save(student02) ;
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 学生转班
	 */
	@Test
	public void testTranformClasses() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Student student04 = (Student)session.get(Student.class, 4L) ;
		Classes classes2 = (Classes)session.get(Classes.class, 2L) ;
		//将学生student04转到2班
		student04.setClasses(classes2) ;
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 解除一个班级和一个学生之间的关系
	 */
	@Test
	public void testReleaseR() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Student student05 = (Student)session.get(Student.class, 5L) ;
		//直接将该学生维护的班级外键cid置空即可
		student05.setClasses(null) ;
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 解除该班级和所有的学生的关系01，多的一方解除关系，效率不高
	 */
	@Test
	public void testReleaseAll01() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Classes classes = (Classes)session.get(Classes.class, 2L) ;
		Set<Student> students = classes.getStudents();
		for (Student student : students) {
			student.setClasses(null) ;
			
		}
		transaction.commit() ;
		session.close() ;
	}
	
	/**
	 * 解除该班级和所有的学生的关系02，一的一方解除关系，效率更高
	 */
	@Test
	public void testReleaseAll02() {
		Session session = sessionFactory.openSession() ;
		Transaction transaction = session.beginTransaction() ;
		Classes classes = (Classes)session.get(Classes.class, 2L) ;
		classes.setStudents(null) ;
		transaction.commit() ;
		session.close() ;
	}
	
}
